/// @tags:
#include <cstdio>
#include <functional>
#include <iostream>
#include <numeric>

using std::cin, std::cout, std::endl;

namespace BlueQuantum {

using ll = long long;

constexpr int maxn = 1e6;

int frac[maxn * 3 + 2], inv[maxn * 3 + 2];

ll qpow(ll base, ll exp, int mod) {
  ll res = 1;
  while (exp) {
    if (exp & 1) res = res * base % mod;
    base = base * base % mod;
    exp >>= 1;
  }
  return res;
}

ll binom(ll n, ll k, int mod) {
  return (ll)frac[n] * inv[k] % mod * inv[n - k] % mod;
}

int main() {
  int n, M;
  cin >> n >> M;
  frac[0] = 1, inv[0] = 1;
  for (int i = 1; i <= maxn * 3; ++i) {
    frac[i] = (ll)frac[i - 1] * i % M;
  }
  inv[maxn * 3] = qpow(frac[maxn * 3], M - 2, M);
  for (int i = maxn * 3; i > 0; --i) {
    inv[i - 1] = (ll)inv[i] * i % M;
  }
  ll ans[4] = {1, 0, 0, 0};
  ans[1] = (2ll * frac[2 * n] - frac[n] + M) % M;
  ans[2] = binom(2 * n, n, M) * frac[2 * n] % M * frac[n] % M * 2 % M;
  for (int i = 0; i <= n; ++i) {
    ll sub = binom(n, i, M) * binom(n, n - i, M) % M * binom(n + i, n, M) % M *
             frac[n] % M * frac[n] % M * frac[n] % M;
    ans[2] = (ans[2] - sub + M) % M;
  }
  ans[3] = frac[3 * n];
  ans[3] = (ans[3] - ans[2] + M) % M;
  ans[2] = (ans[2] - ans[1] + M) % M;
  ans[1] = (ans[1] - ans[0] + M) % M;
  cout << (ans[1] + 2 * ans[2] + 3 * ans[3]) % M << '\n';
  return 0;
}

}  // namespace BlueQuantum

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("E. Partial Sorting.in", "r", stdin);
  freopen("E. Partial Sorting.out", "w", stdout);
#endif
#endif

  std::ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  return BlueQuantum::main();
}
